package server.business.service.Impl;

import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.OverallBloodMapper;
import server.business.pojo.OverallBlood;
import server.business.service.OverallBloodService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Galaxy
 * @description 针对表【overall_blood(整体输血信息表)】的数据库操作Service实现
 * @createDate 2022-11-08 12:46:24
 */
@Service("overallBloodService")
public class OverallBloodServiceImpl extends ServiceImpl<OverallBloodMapper, OverallBlood>
        implements OverallBloodService {

    // *************************************lrz*************************start

    @Autowired
    private OverallBloodMapper overallBloodMapper;

    @Override
    public void setStateByTransfusionNumber(String transfusionNumber, String condition) {
        LambdaUpdateWrapper<OverallBlood> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Strings.isNotBlank(transfusionNumber), OverallBlood::getTransfusionNumber, transfusionNumber);
        updateWrapper.set(OverallBlood::getState, condition);
        int result = overallBloodMapper.update(null, updateWrapper);
        System.out.println("result" + result);
    }
    //*************************************lrz*************************end


    //*************************************zy*************************start

    /**
     * 根据病历号查询是否允许退血
     *
     * @param number 患者的病历号
     * @return 返回整体输血信息
     */
    @Override
    public List<OverallBlood> findIsAllowBackByNumber(Integer number) {
        return overallBloodMapper.findIsAllowBackByNumber(number);
    }

    /**
     * 根据输血申请单号更改整体输血信息表中的状态和单号
     *
     * @param transfusionNumber 输血申请单号
     * @param condition         状态
     * @param number            患者退血信息表中的退血单号
     */
    @Override
    public void updateConditionAndNumberByTransfusionNumber(String transfusionNumber, String condition, String number) {
        overallBloodMapper.updateConditionAndNumberByTransfusionNumber(transfusionNumber, condition, number);
    }

    /**
     * 根据输血申请单号更改整体输血信息表中的状态
     *
     * @param transfusionNumber 输血申请单号
     * @param condition         状态
     */
    @Override
    public void updateConditionByTransfusionNumber(String transfusionNumber, String condition) {
        overallBloodMapper.updateConditionByTransfusionNumber(transfusionNumber, condition);
    }

    /**
     * 根据患者病历号和患者输血申请单号修改是否允许退血
     *
     * @param number      患者病历号
     * @param transfusion 患者输血申请单号
     * @param transfusion 是否允许退血（1表示允许，0表示不允许）
     */
    @Override
    public void updateIsAllowedBackByNumberAndTransfusion(Integer number, String transfusion, Integer isAllowedBack) {
        overallBloodMapper.updateIsAllowedBackByNumberAndTransfusion(number, transfusion, isAllowedBack);
    }

    /**
     * 根据退血单号设置退血驳回原因
     *
     * @param transfusionNumber 输血单号
     * @param rejectInformation 退血驳回原因
     */
    @Override
    public void setRejectInformationByTransfusionNumber(String transfusionNumber, String rejectInformation) {
        overallBloodMapper.setRejectInformationByTransfusionNumber(transfusionNumber, rejectInformation);
    }

    /**
     * 根据血液状态获取血液信息
     *
     * @param condition 血液状态
     * @return 返回退血审核通过的血液信息
     */
    @Override
    public List<OverallBlood> getBloodInformationByCondition(String condition) {
        return overallBloodMapper.getBloodInformationByCondition(condition);
    }

    /**
     * 根据退血单号设置血液状态
     *
     * @param number    退血单号
     * @param condition 血液状态
     */
    @Override
    public void setConditionByNumber(String number, String condition) {
        overallBloodMapper.setConditionByNumber(number, condition);
    }

    /**
     * 根据病历号和输血单号查询输血信息
     *
     * @param number      病历号
     * @param transfusion 输血单号
     * @return 返回输血信息
     */
    @Override
    public OverallBlood findByNumberAndTransfusion(Integer number, String transfusion) {
        return overallBloodMapper.findByNumberAndTransfusion(number, transfusion);
    }

    /**
     * 根据输血单号修改是否允许退血
     *
     * @param transfusion   输血单号
     * @param isAllowedBack 是否允许退血（1表示允许，0表示不允许）
     */
    @Override
    public void updateIsAllowedBackByTransfusion(String transfusion, Integer isAllowedBack) {
        overallBloodMapper.updateIsAllowedBackByTransfusion(transfusion, isAllowedBack);
    }

    /**
     * 分页查询功能
     *
     * @param currentPage  当前页面
     * @param pageSize     页面显示个数
     * @param overallBlood 整体输血信息
     * @return 返回分页数据
     */
    @Override
    public IPage<OverallBlood> getPage(int currentPage, int pageSize, OverallBlood overallBlood) {
        LambdaQueryWrapper<OverallBlood> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(overallBlood.getState()), OverallBlood::getState, overallBlood.getState())
                .eq(Strings.isNotBlank(overallBlood.getPatientId()), OverallBlood::getPatientId, overallBlood.getPatientId())
                .eq(Strings.isNotBlank(overallBlood.getTransfusionNumber()), OverallBlood::getTransfusionNumber, overallBlood.getTransfusionNumber())
                .eq(Strings.isNotBlank(overallBlood.getBackInformationNumber()), OverallBlood::getBackInformationNumber, overallBlood.getBackInformationNumber())
                .eq(Strings.isNotBlank(overallBlood.getMedicalRecordNumber()), OverallBlood::getMedicalRecordNumber, overallBlood.getMedicalRecordNumber())
                .eq(Strings.isNotBlank(overallBlood.getBloodSampleNumber()), OverallBlood::getBloodSampleNumber, overallBlood.getBloodSampleNumber())
                .eq(Strings.isNotBlank(overallBlood.getBloodDispenseNumber()), OverallBlood::getBloodDispenseNumber, overallBlood.getBloodDispenseNumber())
                .eq(overallBlood.getUsageTime() != null, OverallBlood::getUsageTime, overallBlood.getUsageTime())
                .eq(overallBlood.getIsAllowedBack() != null, OverallBlood::getIsAllowedBack, overallBlood.getIsAllowedBack())
                .eq(Strings.isNotBlank(overallBlood.getEvaluationNumber()), OverallBlood::getEvaluationNumber, overallBlood.getEvaluationNumber())
                .eq(Strings.isNotBlank(overallBlood.getAdverseReactionNumber()), OverallBlood::getAdverseReactionNumber, overallBlood.getAdverseReactionNumber())
                .eq(Strings.isNotBlank(overallBlood.getRejectInformation()), OverallBlood::getRejectInformation, overallBlood.getRejectInformation());
        Page<OverallBlood> page = new Page<>(currentPage, pageSize);
        overallBloodMapper.selectPage(page, queryWrapper);
        return page;
    }


    /**
     * 根据编号批量下载选中数据
     *
     * @param response 请求
     * @param ids      编号列表
     */
    @Override
    public void batchDownload(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
        List<OverallBlood> overallBloodList = overallBloodMapper.selectBatchIds(ids);
        // 设置文本内容
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("患者退血退费信息" + System.currentTimeMillis(), "UTF-8");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream(),
                    OverallBlood.class).sheet("患者退血退费信息")
                    .doWrite(overallBloodList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //*************************************zy*************************end
}




